<#include "/macro.include">
<#include "/custom.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.daointerface.${className}DAO" >

    <!-- ======================================================================== 
      COMMON （别名、resultMap、查询字段、sql...）
    ========================================================================= -->
    <!-- <typeAlias alias="${classNameLower}DO" type="${basePackage}.dataobject.${className}DO" /> -->

    <resultMap id="BaseResultMap" type="${basePackage}.dataobject.${className}DO">
    <#list table.columns as column>
        <result column="${column.sqlName}" property="${column.columnNameLower}" />
    </#list>
    </resultMap>

    <sql id="Base_Column_List" >
        <![CDATA[
        <#list table.columns as column>
              ${column.sqlName} <#if column_has_next>,</#if>
        </#list>
        ]]>
    </sql>

    <!-- ========================================================================
      SELECT 
    ========================================================================= -->
    <!--根据ID查询记录-->
    <select id="findById" resultMap="BaseResultMap" parameterType="java.lang.<#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long</#if><#elseif column.javaType == "java.lang.Integer">Integer<#else></#if></#list>" >
      select 
      <include refid="Base_Column_List" />
      <![CDATA[
      from ${table.sqlName}
      where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> = ${"#"}{id}
      ]]>
    </select>
    <#--<!--分页查询&ndash;&gt;-->
    <#--<select id="query${className}Page" parameterType="query${className}Vo"
		resultMap="voResultMap">
		<include refid="Page_SqlMap.pagePrefix"/>
			select 
			<include refid="${classNameLower}.select_column"/>
			<include refid="${classNameLower}.query${className}Page_where"/>
		<include refid="Page_SqlMap.pageSuffix"/>
	</select>-->
	<#--<!--通过Ids查询多条记录&ndash;&gt;
	<select id="getByIds" resultMap="BaseResultMap" parameterType="${classNameLower}">
	   select <include refid="Base_Column_List"/>
	   from ${table.sqlName} where is_delete = 0 and <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> in
	   <iterate property="codes" open="(" close=")" conjunction=",">#codes[]#</iterate>  
	</select>-->

    <!--多条件组合查询-->
    <select id="findListByParams" resultMap="BaseResultMap" parameterType="java.util.Map">
        select <include refid="Base_Column_List"/>
        from ${table.sqlName}
        <where>
        <#list table.columns as column>
            <#if column.javaType == "java.lang.String">
            <if test="${column.columnNameLower} !=null and ${column.columnNameLower}!='' ">
                <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
            </if>
            <#else>
            <if test="${column.columnNameLower} !=null">
                <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
            </if>
            </#if>
        </#list>
        </where>
        <#--<dynamic>
        <#list table.columns as column>
            <isNotEmpty property="${column.columnNameLower}" prepend="and">
                <![CDATA[ ${column.sqlName} = #${column.columnNameLower}# ]]>
            </isNotEmpty>
        </#list>
        </dynamic>-->
    </select>

    <!--分页查询-->
    <select id="findPageListByParams" resultMap="BaseResultMap" parameterType="java.util.Map">
        select <include refid="Base_Column_List"/>
        from ${table.sqlName}
        <where>
        <#list table.columns as column>
            <#if column.javaType == "java.lang.String">
             <if test="${column.columnNameLower} !=null and ${column.columnNameLower}!='' ">
                 <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
             </if>
            <#else>
             <if test="${column.columnNameLower} !=null">
                 <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
             </if>
            </#if>
        </#list>
        </where>
        LIMIT ${"#"}{beginIndex},${"#"}{pageSize}
    </select>

    <!--查询数据条数-->
    <select id="count" parameterType="java.util.Map"  resultType="java.lang.Integer">
        select count(*)
        from ${table.sqlName}
        <where>
        <#list table.columns as column>
            <#if column.javaType == "java.lang.String">
             <if test="${column.columnNameLower} != null and ${column.columnNameLower} !=''">
                 <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
             </if>
            <#else>
             <if test="${column.columnNameLower} != null">
                 <![CDATA[ and ${column.sqlName} = ${"#"}{${column.columnNameLower}} ]]>
             </if>
            </#if>
        </#list>
        </where>
    </select>

    <!-- ========================================================================
      DELETE 
    ========================================================================= -->
    <!--删除-->
    <#--<update id="delete" parameterType="java.lang.<#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long</#if><#elseif column.javaType == "java.lang.Integer">Integer<#else></#if></#list>">
        update ${table.sqlName} set del_flag = '1', del_time = sysdate()
        <![CDATA[
        where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> = ${"#"}{id}
        ]]>
    </update>-->
    <delete id="delete" parameterType="java.lang.<#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long</#if><#elseif column.javaType == "java.lang.Integer">Integer<#else></#if></#list>" >
      <![CDATA[
      delete from ${table.sqlName}
      where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> = ${"#"}{id}
      ]]>
    </delete>

    <!-- ========================================================================
      INSERT 
    ========================================================================= -->
    <!--新增-->
    <insert id="add" parameterType="${basePackage}.dataobject.${className}DO" keyProperty="<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>" useGeneratedKeys="true">
      insert into ${table.sqlName}
        (
        <#list table.columns as column>
            <#if !column.pk>
            ${column.sqlName} <#if column_has_next>,</#if>
            </#if>
        </#list>
        )
        VALUES
        (
        <#list table.columns as column>
            <#if !column.pk>
            <![CDATA[ ${"#"}{${column.columnNameLower}}<#if column_has_next>,</#if>]]>
            </#if>
        </#list>
        )
        <#--<dynamic prepend="(" >
        <#list table.columns as column>
            <isNotNull prepend="," property="${column.columnNameLower}" >
        	${column.sqlName} 
        	</isNotNull>
        </#list>
        )
        </dynamic>
         VALUES
        <dynamic prepend="(" >
        <#list table.columns as column>
            <isNotNull prepend="," property="${column.columnNameLower}" >
        	<![CDATA[ #${column.columnNameLower}#  ]]>
        	</isNotNull>
        </#list>   
        )     
        </dynamic>-->
         
      <#--<selectKey resultType="java.lang.Integer" keyProperty="<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>">
			SELECT LAST_INSERT_ID() as <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list>
	  </selectKey>-->
    </insert>

    <!-- ========================================================================
      UPDATE 
    ========================================================================= -->
    <!--更新-->
    <update id="update" parameterType="${basePackage}.dataobject.${className}DO">
      update ${table.sqlName}
        <set>
         <#list table.columns as column>
         <#if !column.pk>
             <#if column.javaType == "java.lang.String">
             <if test="${column.columnNameLower} != null and ${column.columnNameLower} !=''">
                 <![CDATA[ ${column.sqlName} = ${"#"}{${column.columnNameLower}}<#if column_has_next>,</#if> ]]>
             </if>
             <#else>
             <if test="${column.columnNameLower} != null">
                 <![CDATA[ ${column.sqlName} = ${"#"}{${column.columnNameLower}}<#if column_has_next>,</#if> ]]>
             </if>
             </#if>
         </#if>
         </#list>
        </set>
        <#--<dynamic prepend="set" >
        <#list table.columns as column>
            <#if !column.pk>
        <isNotNull prepend="," property="${column.columnNameLower}" >
          <![CDATA[ ${column.sqlName} = #${column.columnNameLower}# ]]>
        </isNotNull>
            </#if>
        </#list>
      </dynamic>-->
      <![CDATA[
      where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> = ${"#"}{<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>}
      ]]>
    </update>
    
    <#--<!--单条删除（逻辑）&ndash;&gt;
    <update id="logicDel" parameterType="java.lang.Integer">
      <![CDATA[
      update ${table.sqlName}
      set is_delete = 1
      where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> = #id#
       ]]>
    </update>-->
     <#--!--批量删除（逻辑）-->
     <#--<update id="delBatch" parameterType="${classNameLower}Vo">
      update ${table.sqlName}
      set is_delete = 1
      where <#list table.columns as column><#if column.pk>${column.sqlName}</#if></#list> in
      <iterate property="codes" open="(" close=")" conjunction=",">#codes[]#</iterate>
    </update>-->
    <!-- ========================================================================
        自定义sql
      ========================================================================= -->
    
</mapper>


